package ca.concordia.soen6461.wichf.navigation.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.controller.View;
import ca.concordia.soen6461.wichf.entity.PatientCart;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.navigation.common.Payment;
import ca.concordia.soen6461.wichf.navigation.event.PayEvent;
import ca.concordia.soen6461.wichf.navigation.view.CartView.Appointment;

public class ConfirmationView implements View<Payment> {

	public ConfirmationView() {
	}

	@Override
	public Payment render(Event event) {
		PayEvent payEvent = (PayEvent) event;
		DateFormat format = new SimpleDateFormat("MMM dd - HH:mm");
		Payment payment = new Payment();
		PatientCart cart = payEvent.get(PatientCart.class);
		if (cart != null) {
			for (ScheduledEvent appointment : cart.getAppointments()) {
				Appointment a = new Appointment();
				a.setDate(format.format(appointment.getFrom()));
				a.setPrice(appointment.getType().getPrice());
				a.setType(appointment.getType().getDescription());
				payment.getAppointments().add(a);
				payment.setTotal(payment.getTotal() + a.getPrice());
			}
		}
		payment.setCreditCardNumber(hideFirstDigits(payEvent.getCreditCardNumber()));
		payment.setNameOnCard(payEvent.getNameOnCard());
		return payment;
	}

	private String hideFirstDigits(String cardNo) {
		return "XXXX XXXX XXXX " + cardNo.substring(cardNo.length() - 4);
	}

}
